angular.module("config")
    .controller("personalController", function ($scope, $http, $location,md5) {
        $scope.oldPwd = "";
        $scope.c = {};
        $scope.submit = function () {

            var length = 0;
            for (var i in $scope.c) {
                length++;
            }

            if (length > 0) {//$scope.c != null||$scope.c!=={}
                console.log("...." + $scope.c.length);
                var req = {
                    method: "PUT",
                    url: "/client",
                    data: $scope.c,
                    headers: {"Content-type": "application/json"}
                }
                $http(req).then(function (res) {
                    console.log("submit...." + res.data);
                    alert("用户信息修改成功");
                })
            }
            else {
                alert("请输入您要修改的信息");
            }
        }
        var getTelFromSession = function ($scope, $http) {
            var req = {
                method: "get",
                url: "/client"
            }
            $http(req).then(function (res) {
                $scope.tel = (res.data.cTel);
                $scope.c.cPersonName = res.data.cPersonName;
                $scope.c.cLocation = res.data.cLocation;
            })
        }
        getTelFromSession($scope, $http);

        var verifyPwd = 0;
        var verifyPwd2 = 0;
        var verifyPwd3 = 0;
        $scope.verifyPwd1 = function () {
            console.log(".......oldPwd");
            $scope.$watch('oldPwd', function (n, o) {
                var req = {
                    method: "post",
                    url: "/client/login",
                    data: {
                        cTel: $scope.tel,
                        cPassword: md5.createHash($scope.oldPwd || "")
                    }
                }
                $http(req).then(function (res) {
                    if (!res.data.error) {
                        verifyPwd = 1;
                        $scope.oldPwd_checker1 = null;
                    }
                    else {
                        verifyPwd = 0;
                        $scope.oldPwd_checker1 = "请输入正确的密码";
                    }
                });
            }, true)
        }

        console.log("out ..confirmPwd...." + verifyPwd);

        $scope.verifyPwd2 = function () {
            $scope.$watch('newPwd1', function (n, o) {
                if ($scope.newPwd1 != null) {
                    $scope.newPwd_checker2 = null;
                } else {
                    $scope.newPwd_checker2 = "新密码不为空";
                }
                if($scope.newPwd1.length>6 ){
                    verifyPwd3=1;
                }
                else {
                    verifyPwd3=0;
                    $scope.newPwd_checker2="输入密码长度不得小于6";
                }
            })
        }

        $scope.verifyPwd3 = function () {
            $scope.$watch('newPwd2', function (n, o) {
                if ($scope.newPwd2 != "" && $scope.newPwd1 == $scope.newPwd2) {
                    verifyPwd2 = 1;
                    $scope.newPwd_checker3 = null;
                } else {
                    verifyPwd2 = 0;
                    $scope.newPwd_checker3 = "新密码两次输入不一致";
                }
            })
        }

        $scope.confirmPwd = function () {
            console.log("in..confirmPwd...." + verifyPwd);
            if (verifyPwd == 1 && verifyPwd2 == 1&&verifyPwd3==1) {
                var req = {
                    method: "put",
                    url: "/client",
                    data: {
                        cTel: $scope.tel,
                        cPassword: md5.createHash($scope.newPwd1 || "")
                    }
                }
                $http(req).then(function (res) {
                    if (!res.data.error) {
                        $("#PwdModal").modal("hide");
                        $scope.newPwd1 = null;
                        $scope.newPwd2 = null;
                        $scope.oldPwd = null;
                        alert("密码修改成功");
                    }
                    else {
                        alert("密码修改失败，请重试");
                    }
                })
            }
        }

        $scope.$on('UserLogout', function (event, data) {
            $location.path("#/");
        });


        $scope.$emit("changeTopStyle", true);
    });